package com.laiyima.zhongjiang.linghuilv.demo;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.util.PrivacyActivity;
import com.laiyima.zhongjiang.linghuilv.demo.util.UserAgreementActivity;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class RegisterActivity extends Activity implements View.OnClickListener{
    private EditText Number,Password,agai_password,input_name;
    private Button Send_register,Next;
   // private CheckBox myCheckbox;
    private ImageView register_image;
     private int Registercode;
    private String TAG="RegisterActivity";
    private boolean click_Checkbox=false;
    private ImageView mImageView;
    private TextView mTextVIew,to_priacy;

    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        input_name=findViewById(R.id.input_name);
        Drawable drawable5 = getResources().getDrawable(R.drawable.radio_wode);
        drawable5.setBounds(0, 0, 50, 60);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        input_name.setCompoundDrawables(drawable5, null, null, null);//只

        Number=findViewById(R.id.re_number);
        Drawable drawable1 = getResources().getDrawable(R.drawable.shouji);
        drawable1.setBounds(0, 0, 50, 60);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        Number.setCompoundDrawables(drawable1, null, null, null);//只

        Password=findViewById(R.id.re_password);
        Drawable drawable = getResources().getDrawable(R.drawable.mima);
        drawable.setBounds(0, 0, 60, 60);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        Password.setCompoundDrawables(drawable, null, null, null);//只

        agai_password=findViewById(R.id.re_again_password);
        Drawable drawable3 = getResources().getDrawable(R.drawable.mima);
        drawable3.setBounds(0, 0, 60, 60);//第一0是距左边距离，第二0是距上边距离，40分别是长宽
        agai_password.setCompoundDrawables(drawable3, null, null, null);//只

        Next=findViewById(R.id.re_next);
        mImageView=findViewById(R.id.re_back);
        Next.setOnClickListener(this);
        Number.setInputType(EditorInfo.TYPE_CLASS_PHONE);

        input_name.setCompoundDrawablePadding(40);
        Number.setCompoundDrawablePadding(40);
        Password.setCompoundDrawablePadding(40);
        agai_password.setCompoundDrawablePadding(40);

        register_image=findViewById(R.id.register_image);
        register_image.setOnClickListener(this);


        mTextVIew=findViewById(R.id.user_agreement);
        mTextVIew.setOnClickListener(this);
        to_priacy=findViewById(R.id.to_priacy);

        to_priacy.setOnClickListener(this);

        mImageView.setOnClickListener(this);







        Next.setEnabled(false);
        Number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (Number.length()==11 && Password.length()>5 && agai_password.length()>5){
                    Next.setEnabled(true);
                    Next.setTextColor(Color.WHITE);
                    Next.setBackgroundResource(R.drawable.begin_textview_shape_change);
                }
                else{
                    Next.setEnabled(false);
                    Next.setTextColor(Color.parseColor("#B8B8B8"));
                    Next.setBackgroundResource(R.drawable.firist_edittext_shape);
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        Password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (Number.length()==11 && Password.length()>5 && agai_password.length()>5){
                    Next.setEnabled(true);
                    Next.setTextColor(Color.WHITE);
                    Next.setBackgroundResource(R.drawable.begin_textview_shape_change);
                }
                else{
                    Next.setEnabled(false);
                    Next.setTextColor(Color.parseColor("#B8B8B8"));
                    Next.setBackgroundResource(R.drawable.firist_edittext_shape);
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        agai_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (Number.length()==11 && Password.length()>5 && agai_password.length()>5){
                    Next.setEnabled(true);
                    Next.setTextColor(Color.WHITE);
                    Next.setBackgroundResource(R.drawable.begin_textview_shape_change);
                }
                else{
                    Next.setEnabled(false);
                    Next.setTextColor(Color.parseColor("#B8B8B8"));
                    Next.setBackgroundResource(R.drawable.firist_edittext_shape);
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        setStatusBar();

    }





    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.register_image:
                if(click_Checkbox==false){
                    click_Checkbox=true;
                    register_image.setImageResource(R.drawable.cb_true);
                }else {
                    click_Checkbox=false;
                    register_image.setImageResource(R.drawable.cb_fase);
                }


                break;
            case R.id.to_priacy:
                Intent Privacyintent =new Intent(RegisterActivity.this, PrivacyActivity.class);
                startActivity(Privacyintent);
                break;
            case R.id.user_agreement:
                Intent UAintent =new Intent(RegisterActivity.this, UserAgreementActivity.class);
                startActivity(UAintent);
                break;
            case R.id.re_back:
                this.finish();
                break;
            case R.id.re_next:
                  String mobile = Number.getText().toString().trim();
                  String password = Password.getText().toString().trim();
                  String name=input_name.getText().toString();
                String again_password=agai_password.getText().toString().trim();
            if (!judgePhoneNums(mobile)) {
                return;
            } else if (password.length() < 6) {
                Toast.makeText(this, "请输入长度大于6位的密码", Toast.LENGTH_SHORT).show();
                return;
            }
            else if (name.length() < 6) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                return;
            }
            else if (password.equals(again_password)==false) {
                Toast.makeText(this, "请再次输入一样密码", Toast.LENGTH_SHORT).show();
                return;
            }else if (click_Checkbox == false) {
                Toast.makeText(this, "请阅读相关协议", Toast.LENGTH_SHORT).show();
                return;
            } else {
                SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE); //私有数据
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("mobile", mobile);
                editor.putString("password", password);
                editor.putString("name", name);
                editor.commit();
                    // 这里的服务器接口是获取验证码的接口地址
                    RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/api/register/sms");
                    //需要携带的参数
                    params.addBodyParameter("mobile", mobile);
                  //  params.addBodyParameter("code", str);
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            try{
                                JSONObject jsonObject = new JSONObject(result);
                                Registercode = jsonObject.optInt("code", 0);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (Registercode==200){
                                Intent Captchaintent = new Intent();
                                Captchaintent=new Intent(RegisterActivity.this,CaptchaActivity.class);
                                startActivity(Captchaintent);
                            }
                            if (Registercode==-1){
                                  Toast.makeText(getBaseContext().getApplicationContext(), "手机号码已注册", Toast.LENGTH_SHORT).show();
                            }
                            if (Registercode==500){
                                Toast.makeText(getBaseContext().getApplicationContext(), "错误的手机号码", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(org.xutils.common.Callback.CancelledException cex) {
                        }

                        @Override
                        public void onFinished() {
                        }

                    });
                break;
            }
        }
    }

        private boolean judgePhoneNums (String phoneNums){
            if (isMatchLength(phoneNums, 11) && isMobileNO(phoneNums)) {
                return true;
            }
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return false;
        }

        /**
         * 判断一个字符串的位数
         * @param str
         * @param length
         * @return
         */
        public static boolean isMatchLength (String str,int length){
            if (str.isEmpty()) {
                return false;
            } else {
                return str.length() == length ? true : false;
            }
        }

        /**
         * 验证手机格式
         */
        public static boolean isMobileNO (String mobileNums){
            String strTel = "[1][358]\\d{9}";
            if (TextUtils.isEmpty(mobileNums))
                return false;
            else
                return mobileNums.matches(strTel);
        }


}
